package dejur.projectutil;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.MappingException;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.SQLServerDialect;

import controllers.CRUD;

public class ControllersGenerator {

	public static void main(String[] args) throws MappingException,
			ClassNotFoundException, IOException {

		File controllersFolder = new File(
				"C:/Projetos/DEJUR/DEJUR_WEB/app/controllers/");
		File[] controllers = controllersFolder.listFiles();
		for (File file : controllers) {
			if (!file.getName().equals("Application.java")) {
				file.delete();
			}
		}

		File modelsFolder = new File("C:/Projetos/DEJUR/DEJUR_WEB/app/models");
		File[] entities = modelsFolder.listFiles();
		for (int i = 0; i < entities.length; i++) {

			File entityJavaFile = entities[i];
			String entity = entityJavaFile.toString();
			if (entity.endsWith(".java")) {

				entity = StringUtils.substringBeforeLast(
						StringUtils.substringAfterLast(
								entityJavaFile.toString(), "\\"), ".");

				StringBuilder builder = new StringBuilder();
				builder.append("package controllers;"
						+ System.getProperty("line.separator"));
				builder.append(System.getProperty("line.separator"));
				builder.append("public class " + entity + "s extends CRUD {}");
				File newFile = new File(
						"C:/Projetos/DEJUR/DEJUR_WEB/app/controllers/" + entity
								+ "s.java");
				FileUtils.writeStringToFile(newFile, builder.toString());
				System.out.println(newFile.getAbsolutePath());
			}
		}
	}
}
